package logic

import (
	"context"
	"errors"
	"zf/models/mysql"

	"zf/zfrpc/internal/svc"
	"zf/zfrpc/zfrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodHomeAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodHomeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodHomeAddLogic {
	return &GoodHomeAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodHomeAddLogic) GoodHomeAdd(in *zfrpc.GoodHomeAddRequest) (*zfrpc.GoodHomeAddResponse, error) {
	// todo: add your logic here and delete this line
	goodHome := mysql.GoodHome{
		Name:    in.Name,
		Status:  in.Status,
		Image:   in.Image,
		Address: in.Address,
		Info:    in.Info,
	}
	if in.Status != "上架" {
		return nil, errors.New("当前未上架,服务不可预定")
	}
	if in.Name == goodHome.Name {
		return nil, errors.New("存在同类型上架,支持合并")
	}

	mysql.DB.Debug().Create(&goodHome)
	return &zfrpc.GoodHomeAddResponse{
		GoodHomeId: int64(goodHome.ID),
	}, nil
}
